<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class Exam extends AdminBase
{

    protected $classes_model;
    protected $exam_model;

    protected $admin_user_model;

    public function __construct()
    {
        parent::__construct();
        $this->classes_model = new \app\common\model\Classes();
        $this->exam_model = new \app\common\model\Exam();
        $this->admin_user_model = new \app\common\model\AdminUser();
    }


    /**
     * 考试管理
     * @param $class_id
     * @param $exam_name
     * @param $page
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index($class_id = 0,$exam_name = '', $page = 1){

        $map = [];

        if ($class_id > 0) {
            $map['class_id'] = $class_id;
        }

        if (!empty($exam_name)) {
            $map['exam_name'] = ['like', "%{$exam_name}%"];
        }

        $class_list = $this->classes_model->all();

        $exam_list = $this->exam_model->where($map)->order('id DESC')
            ->paginate(10, false, ['page' => $page])->each(function($item, $key){
                $class_id = $item['class_id'];
                $class = $this->classes_model->find($class_id);
                $item['class_name'] = $class['class_name'];

                $user_id = $item['user_id'];
                $user = $this->admin_user_model->find($user_id);
                $item['username'] = $user['username'];
            });

        return $this->fetch('index', [
            'class_list' => $class_list,
            'exam_list' => $exam_list,
            'exam_name' => $exam_name,
            'class_id' => $class_id,
        ]);
    }

    /**
     * 添加考试
     * @return mixed|void
     * @throws \think\exception\DbException
     */
    public function add(){
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result_validate = $this->validate($data, 'Exam');
            if (true !== $result_validate) {
                $this->error($result_validate);
            } else {
                $result = $this->exam_model->save($data);
                if ($result) {
                    $this->success("保存成功");
                } else {
                    $this->error('保存失败');
                }
            }
        } else {
            $class_list = $this->classes_model->all();
            return $this->fetch('add',['class_list' => $class_list]);
        }
    }

    /**
     * 编辑考试
     * @param $id
     * @return void
     */
    public function edit($id = 0)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $result_validate = $this->validate($data, 'Exam');
            if (true !== $result_validate) {
                $this->error($result_validate);
            } else {
                $result = $this->exam_model->save($data, $id);
                if ($result) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        } else {
            $exam = $this->exam_model->find($id);
            $class_list = $this->classes_model->all();
            return $this->fetch('edit', ['exam' => $exam, 'class_list' => $class_list]);
        }
    }

    /**
     * 删除考试
     * @param $id
     * @return void
     */
    public function delete($id = 0)
    {
        if ($id !== 0 ) {
            $result = $this->exam_model->destroy($id);
            if ($result) {
                $this->success('删除成功');
            } else {
                $this->error('删除失败！err-1');
            }
        } else {
            $this->error("删除失败！err-2");
        }
    }
}